package api

import (
	"blog/models"
	"blog/utils"
	"github.com/astaxie/beego/validation"
	"strconv"
)

//  AdminController operations for Admin
type AdminController struct {
	//beego.Controller
	BaseController
}

type Admin struct {
	Id        int64
	Account   string
	Password  string
	Avatar    string
	Nickname  string
	Introduce string
}

func (a *Admin) Valid(v *validation.Validation) {

}

// GetOne ...
// @Title Get One
// @Description get Admin by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.Admin
// @Failure 403 :id is empty
// @router /:id [get]
func (c *AdminController) GetOne() {
	idStr := c.Ctx.Input.Param(":id")
	id, _ := strconv.ParseInt(idStr, 0, 64)
	v, err := models.GetAdminById(id)
	if err != nil {
		c.Data["json"] = map[string]interface{}{"code": 2, "message": "信息不存在"}
	} else {
		c.Data["json"] = map[string]interface{}{"code": 0, "message": "操作成功", "data": v}
	}
	c.ServeJSON()
}

func (c *AdminController) Put() {
	idStr := c.Ctx.Input.Param(":id")
	id, _ := strconv.ParseInt(idStr, 0, 64)
	Account := c.GetString("account")
	Nickname := c.GetString("nickname")
	Password := c.GetString("password")
	Introduce := c.GetString("introduce")
	Avatar := c.GetString("avatar")

	Password = utils.GetMd5String(Password)

	admin := models.Admin{Id: id}
	admin.Account = Account
	admin.Nickname = Nickname
	admin.Password = Password
	admin.Introduce = Introduce
	admin.Avatar = Avatar

	if err := models.UpdateAdminById(&admin); err == nil {
		c.Data["json"] = map[string]interface{}{"code": 0, "message": "操作成功"}
	} else {
		c.Data["json"] = map[string]interface{}{"code": 1, "message": "操作失败"}

	}

	c.ServeJSON()
}
